package code.hibernate.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;


import code.hibernate.po.*;
import hibernate.HibernateBaseDAO;



public class CodeDAO extends HibernateBaseDAO {
	HibernateBaseDAO dao=HibernateBaseDAO.getInstance();
	private static final Log log = LogFactory.getLog(CodeDAO.class);
	// property constants
	public static final String CODENAME = "codename";
	public static final String CODEVALUE = "codevalue";
	public static final String CODETYPENAME = "codetypename";


	public Code findById(java.lang.Integer id) {
		log.debug("getting KeyanCode instance with id: " + id);
		try {
			Code instance = (Code) getSession().get(
					Code.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}


	public List findByProperty(String propertyName, Object value) {
		log.debug("finding KeyanCode instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from KeyanCode as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}


	public List findAll() {
		log.debug("finding all KeyanCodetype instances");
		try {
			String queryString = "from KeyanCode";
			return dao.find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List getCodeListByType(String codetypeName){
		
		List result=null;
		String hql = "From KeyanCode as c where c.codetypename='"+codetypeName+"' order by c.codevalue";
		result=dao.find(hql);
		return result;
	}
	//将一整条记录删除
	public boolean delete(Object o){
		return dao.delete(o);
	}
	public void save(Object o){
		try{
			dao.saveOrupdate(o);
		}
		catch(HibernateException e){
			 e.printStackTrace();
			}
	}
}